﻿/*****************************************************************
* Project: HFW Framework
*****************************************************************
* Developer: Hdhoan
* Date created: 11/2010
* Modified:
******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace HFW.Share
{
    public class Global
    {
        #region Number to Text

        /// <summary>
        /// Phương thức đổi số tiền thành chữ.
        /// Hdhoan
        /// </summary>
        /// <param name="sodoi">Số tiền cần đổi thành chữ</param>
        /// <returns>Ex: 10.000 . Return : Mười ngàn đồng.</returns>
        public static string Number2Text(long sodoi)
        {
            string strsodoi = sodoi.ToString();
            int sodai = strsodoi.Length;
            int lan = 0;
            string textsodoi = "";
            string[] textchuoi = new string[20];
            for (int i = 0; i < sodai; i += 3)
            {
                if (i > sodai) break;
                string temptext = layMid(sodoi.ToString(), i, i + 3);
                string kyhieu = "";
                bool conso = true; bool consosau = false; bool phancach = false;
                int sochia = (int)i / 9; int sodu = (int)(i % 9) / 3;
                if (i == 0) { kyhieu = "đồng"; consosau = false; phancach = true; }
                //	if (i==3) kyhieu="ngàn";//	if (i==6) kyhieu="triệu";//	if (i==9) kyhieu="tỷ";
                if (sochia > 0 && sodu == 0) { kyhieu = "tỷ"; phancach = true; }
                if (sodu == 1) { kyhieu = "ngàn"; }
                if (sodu == 2) { kyhieu = "triệu"; }
                if (i + 3 > sodai) { conso = false; }
                if (i > 0) { consosau = true; }
                textchuoi[lan] = " " + doi3so(Convert.ToInt32(temptext), conso, consosau, phancach, kyhieu).Trim();
                //  textsodoi+=doi3so(Convert.ToInt32(temptext),conso,consosau,phancach,kyhieu);
                lan++;
            }
            textsodoi = "";
            for (int i = textchuoi.Length - 1; i >= 0; i--) { textsodoi += textchuoi[i]; }
            string ch = textsodoi.Trim().Substring(0, 1);
            string ch2 = textsodoi.Trim().Substring(1);
            string ch3 = ch.ToUpper() + ch2 + ".";
            return ch3;
        }

        private static string doi3so(long sodoi, bool consotruoc, bool consosau, bool phancach, string kyhieu)
        {
            int donvi;
            int chuc;
            int tram;
            string tkyhieu = kyhieu;
            donvi = (int)sodoi % 10;
            chuc = (int)(sodoi % 100 - donvi) / 10;
            tram = (int)sodoi / 100;
            string texttram;
            int[] aso = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            string[] satram = { "không trăm", "một trăm", "hai trăm", "ba trăm", "bốn trăm", "năm trăm", "sáu trăm", "bảy trăm", "tám trăm", "chín trăm" };
            string[] sachuc = { "lẻ", "mười", "hai mươi", "ba mươi", "bốn mươi", "năm mươi", "sáu mươi", "bảy mươi", "tám mươi", "chín mươi" };
            string[] sadonvi = { "", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
            int vttram = Array.IndexOf(aso, tram);
            int vtchuc = Array.IndexOf(aso, chuc);
            int vtdonvi = Array.IndexOf(aso, donvi);
            texttram = satram[vttram];
            string textchuc = sachuc[vtchuc];
            string textdonvi = sadonvi[vtdonvi];
            if (chuc > 0 && donvi == 5) { textdonvi = "lăm"; }
            if (chuc > 1 && donvi == 1) { textdonvi = "mốt"; }
            if (tram > 0 && chuc == 0 && donvi == 0) { textchuc = ""; textdonvi = ""; }
            if (tram == 0 && chuc == 0 && donvi == 0) { texttram = ""; textchuc = ""; textdonvi = ""; }
            if (consotruoc != true && tram == 0 && chuc == 0 && donvi == 0) { texttram = "không"; textchuc = ""; textdonvi = ""; }
            if (consotruoc != true && tram == 0 && chuc == 0 && donvi > 0) { texttram = ""; textchuc = ""; }
            if (consotruoc != true && tram == 0 && (chuc > 0 || donvi > 0)) { texttram = ""; }
            if (consotruoc == true && consosau == true && phancach != true && tram == 0 && chuc == 0 && donvi == 0) { tkyhieu = ""; }
            return " " + texttram + " " + textchuc + " " + textdonvi + " " + tkyhieu;
        }

        private static string layMid(string chuoi, int vtbd, int vtkt)
        {
            int tvtbd = vtbd;
            int tvtkt = vtkt;
            if (tvtbd < 0) tvtbd = 0;
            if (tvtkt > chuoi.Length) tvtkt = chuoi.Length;
            string rchuoi = layRight(chuoi, tvtkt);
            rchuoi = layLeft(rchuoi, tvtkt - tvtbd);
            return rchuoi;
        }

        private static string layRight(string chuoi, int sokytu)
        {
            int dai = chuoi.Length;
            int tsokt = sokytu;
            if (tsokt > dai)
                tsokt = dai;
            return chuoi.Substring(dai - tsokt, tsokt);
        }

        private static string layLeft(string chuoi, int sokytu)
        {
            int dai = chuoi.Length;
            int tsokt = sokytu;
            if (tsokt > dai)
                tsokt = dai;
            return chuoi.Substring(0, tsokt);
        }

        public static string ConvertNumberToText(string number)
        {
            NumberToText _textNumber = new NumberToText();
            return _textNumber.Convert(number, '.', "phẩy");
        }

        public class NumberToText // convert a number to text
        {
            private string[] strSo = { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
            private string[] strDonViNho = { "linh", "lăm", "mười", "mươi", "mốt", "trăm" };
            private string[] strDonViLon = { "", "ngàn", "triệu", "tỷ" };
            private string[] strMainGroup;
            private string[] strSubGroup;
            private string Len1(string strA)
            {
                return strSo[int.Parse(strA)];
            }
            private string Len2(string strA)
            {
                if (strA.Substring(0, 1) == "0")
                {
                    return strDonViNho[0] + " " + Len1(strA.Substring(1, 1));
                }
                else if (strA.Substring(0, 1) == "1")
                {
                    if (strA.Substring(1, 1) == "5")
                    {
                        return strDonViNho[2] + " " + strDonViNho[1];
                    }
                    else if (strA.Substring(1, 1) == "0")
                    {
                        return strDonViNho[2];
                    }
                    else
                    {
                        return strDonViNho[2] + " " + Len1(strA.Substring(1, 1));
                    }
                }
                else
                {
                    if (strA.Substring(1, 1) == "5")
                    {
                        return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + strDonViNho[1];
                    }
                    else if (strA.Substring(1, 1) == "0")
                    {
                        return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3];
                    }
                    else if (strA.Substring(1, 1) == "1")
                    {
                        return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + strDonViNho[4];
                    }
                    else
                    {
                        return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + Len1(strA.Substring(1, 1));
                    }
                }
            }
            private string Len3(string strA)
            {
                if ((strA.Substring(0, 3) == "000"))
                {
                    return null;
                }
                else if ((strA.Substring(1, 2) == "00"))
                {
                    return Len1(strA.Substring(0, 1)) + " " + strDonViNho[5];
                }
                else
                {
                    return Len1(strA.Substring(0, 1)) + " " + strDonViNho[5] + " " + Len2(strA.Substring(1, strA.Length - 1));
                }
            }

            private string FullLen(string strSend)
            {
                bool boKTNull = false;
                string strKQ = "";
                string strA = strSend.Trim();
                int iIndex = strA.Length - 9;
                int iPreIndex = 0;

                if (strSend.Trim() == "")
                {
                    return Len1("0");
                }
                //tra ve khong neu la khong
                for (int i = 0; i < strA.Length; i++)
                {
                    if (strA.Substring(i, 1) != "0")
                    {
                        break;
                    }
                    else if (i == strA.Length - 1)
                    {
                        return strSo[0];
                    }
                }
                int k = 0;
                while (strSend.Trim().Substring(k++, 1) == "0")
                {
                    strA = strA.Remove(0, 1);
                }
                //
                if (strA.Length < 9)
                {
                    iPreIndex = strA.Length;
                }
                //
                if ((strA.Length % 9) != 0)
                {
                    strMainGroup = new string[strA.Length / 9 + 1];
                }
                else
                {
                    strMainGroup = new string[strA.Length / 9];
                }
                //nguoc
                for (int i = strMainGroup.Length - 1; i >= 0; i--)
                {
                    if (iIndex >= 0)
                    {
                        iPreIndex = iIndex;
                        strMainGroup[i] = strA.Substring(iIndex, 9);
                        iIndex -= 9;
                    }
                    else
                    {
                        strMainGroup[i] = strA.Substring(0, iPreIndex);
                    }

                }
                /////////////////////////////////
                //tach moi maingroup thanh nhieu subgroup
                //xuoi
                for (int j = 0; j < strMainGroup.Length; j++)
                {
                    //gan lai gia tri
                    iIndex = strMainGroup[j].Length - 3;
                    if (strMainGroup[j].Length < 3)
                    {
                        iPreIndex = strMainGroup[j].Length;
                    }
                    ///
                    if ((strMainGroup[j].Length % 3) != 0)
                    {
                        strSubGroup = new string[strMainGroup[j].Length / 3 + 1];
                    }
                    else
                    {
                        strSubGroup = new string[strMainGroup[j].Length / 3];
                    }
                    for (int i = strSubGroup.Length - 1; i >= 0; i--)
                    {
                        if (iIndex >= 0)
                        {
                            iPreIndex = iIndex;
                            strSubGroup[i] = strMainGroup[j].Substring(iIndex, 3);
                            iIndex -= 3;
                        }
                        else
                        {
                            strSubGroup[i] = strMainGroup[j].Substring(0, iPreIndex);
                        }
                    }
                    //duyet subgroup de lay string
                    for (int i = 0; i < strSubGroup.Length; i++)
                    {
                        boKTNull = false;//phai de o day
                        if ((j == strMainGroup.Length - 1) && (i == strSubGroup.Length - 1))
                        {
                            if (strSubGroup[i].Length < 3)
                            {
                                if (strSubGroup[i].Length == 1)
                                {
                                    strKQ += Len1(strSubGroup[i]);
                                }
                                else
                                {
                                    strKQ += Len2(strSubGroup[i]);
                                }
                            }
                            else
                            {
                                strKQ += Len3(strSubGroup[i]);
                            }
                        }
                        else
                        {
                            if (strSubGroup[i].Length < 3)
                            {
                                if (strSubGroup[i].Length == 1)
                                {
                                    strKQ += Len1(strSubGroup[i]) + " ";
                                }
                                else
                                {
                                    strKQ += Len2(strSubGroup[i]) + " ";
                                }
                            }
                            else
                            {
                                if (Len3(strSubGroup[i]) == null)
                                {
                                    boKTNull = true;
                                }
                                else
                                {
                                    strKQ += Len3(strSubGroup[i]) + " ";
                                }
                            }
                        }
                        //dung
                        if (!boKTNull)
                        {
                            if (strSubGroup.Length - 1 - i != 0)
                            {
                                strKQ += strDonViLon[strSubGroup.Length - 1 - i] + " ";
                            }
                            else
                            {
                                strKQ += strDonViLon[strSubGroup.Length - 1 - i] + " ";
                            }

                        }
                    }
                    //dung
                    if (j != strMainGroup.Length - 1)
                    {
                        if (!boKTNull)
                        {
                            strKQ = strKQ.Substring(0, strKQ.Length - 1) + strDonViLon[3] + " ";
                        }
                        else
                        {
                            strKQ = strKQ.Substring(0, strKQ.Length - 1) + " " + strDonViLon[3] + " ";
                        }
                    }
                }
                //xoa ky tu trang
                strKQ = strKQ.Trim();
                //xoa dau , neu co
                if (strKQ.Substring(strKQ.Length - 1, 1) == ".")
                {
                    strKQ = strKQ.Remove(strKQ.Length - 1, 1);
                }
                return strKQ;

                ////////////////////////////////////


            }
            public string Convert(string strSend, char charInSeparator, string strOutSeparator)
            {
                if (strOutSeparator == "")
                {
                    return "Lỗi dấu phân cách đầu ra rỗng";
                }
                if (strSend == "")
                {
                    return Len1("0");
                }

                string[] strTmp = new string[2];
                try
                {

                    strTmp = strSend.Split(charInSeparator);
                    string strTmpRight = strTmp[1];
                    for (int i = strTmpRight.Length - 1; i >= 0; i--)
                    {
                        if (strTmpRight.Substring(i, 1) == "0")
                        {
                            strTmpRight = strTmpRight.Remove(i, 1);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (strTmpRight != "")
                    {
                        string strRight = "";
                        for (int i = 0; i < strTmpRight.Length; i++)
                        {
                            strRight += Len1(strTmpRight.Substring(i, 1)) + " ";
                        }


                        return FullLen(strTmp[0]) + " " + strOutSeparator + " " + strRight.TrimEnd();
                    }
                    else
                    {
                        return FullLen(strTmp[0]);
                    }
                }
                catch
                {
                    return FullLen(strTmp[0]);
                }

            }

        }
        #endregion
        public const int DEFAULT_SLEEPTIME = 100;  
        #region API Support Methods

        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public short wYear;
            public short wMonth;
            public short wDayOfWeek;
            public short wDay;
            public short wHour;
            public short wMinute;
            public short wSecond;
            public short wMilliseconds;
        }
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetSystemTime(ref SYSTEMTIME theDateTime);

        [DllImport("kernel32.dll", EntryPoint = "GetLastError")]
        public static extern int GetLastError();

        #endregion
    }
    
    public static class Applications
    {
        public static string CURRENT_USER_ID = "ADMIN";
        public static string CURRENT_USER_NAME = "Administrator";
        public static string CURRENT_PATH = System.Windows.Forms.Application.StartupPath;
        public static string EXTENSION = ".doc";
        public static string DEFAULT_QUANTITY_UNIT = "Cái";
        public static string DEFAULT_WEIGHT_UNIT = "Gram";
        public static string DEFAULT_MONEY_UNIT = "VNĐ";
        public static string CURRENT_MESSAGE = string.Empty;
        public static string APPLICATION_NAME = "HFW Framework";
        public static string APPLICATION_TITLE_MESSAGEBOX = "HFW Framework";
    }
}
